/* *********
 *    Author: Jason He;
 *    Version: 0.0.1;
 *    Date: 2016-12-26;
 *    File: list.js;
 ********* */

$(document).ready(function () {
	$("#header-switch-btn").click(function () {
		$(this).toggleClass("big");
		$("#list").toggleClass("big");
	});

	$(".nav-btn").click(function () {
		$("body").scrollTop(44);
		$(".list-popup").css("visibility", "hidden");
		if ($(this).hasClass("active")) {
			$(this).removeClass("active");
		} else {
			$(".nav-btn").removeClass("active");
			$(this).addClass("active");
			var id = $(this).data("id");
			$("#" + id).css("visibility", "visible");
		}
	});

	$('#price-slider').nstSlider({
		"left_grip_selector": "#price-leftGrip",
		"right_grip_selector": "#price-rightGrip",
		"value_bar_selector": "#price-bar",
		"value_changed_callback": function (cause, leftValue, rightValue) {
			if (leftValue == 0 && rightValue == 50) {
			    $("#price-cur").text("不限");
			    $("#price-cur").attr("curvalue","");
			} else if (leftValue > 0 && rightValue == 50) {
			    $("#price-cur").text(leftValue + "万以上");
			    $("#price-cur").attr("curvalue", ">" + leftValue );
			} else if (leftValue == 0 && rightValue < 50) {
			    $("#price-cur").text(rightValue + "万以下");
			    $("#price-cur").attr("curvalue", "<" + rightValue);
			} else if (leftValue > 0 && rightValue < 50) {
			    $("#price-cur").text(leftValue + "-" + rightValue + "万");
			    $("#price-cur").attr("curvalue", leftValue + "-" + rightValue);
			}
		}
	});
	$(".filterkey").click(function () {
	    var key = $(this).attr('name');
	    if ($(this).attr('fullvalue'))
	    {
	        var sub = this.innerText;
	        var arr = $(this).attr('fullvalue').split(',');
	        var newv = '';
	        for (var i = 0; i < arr.length; i++)
	        {
	            if (arr[i] != sub)
	            {
	                if (newv != '')
	                    newv += ',';
	                newv += arr[i];
	            }
	        }
	        jumpUrl('', key, newv);
	    }
        else
	        jumpUrl('', key, '');
	});
	$(window).scroll(function () {
	    var $this = $(this),
        viewH = $(this).height(),
	    contentH = Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
	    scrollTop = $(this).scrollTop();
	    var ajaxurl = jumpAjax();
	    if (ajaxurl == "")
	    {
	        $(".loading").show();
	        return;
	    }
        
	    if (scrollTop / (contentH - viewH) >= 0.95 && lockajax == false) {
	        lockajax = true;
	        $.ajax({
	            url: ajaxurl,
	            type: "get",
	            async: false,
	            dataType: 'json',
	            success: function (res) {
	                pagenum++;
	                var data = res.Data;
	                for (var i = 0; i < data.length; i++) {
	                    var car = data[i];
	                    console.log(car);
	                    var imgtag = '<img src="" >';
	                    if(car.imgs != '')
	                    {
	                        var imgs = car.imgs.split(",");
	                        imgtag = '<img src="/Public/Uploads/img/' + imgs[0] + '_cn.png">';
	                    }
	                    var html = '<li><a class="clearfix" href="/index.php?m=App&c=Index&a=car&id=' + car.id
                            + '">' + imgtag +
	                    '<p class="list-title">' + car.na +
                        '</p><p class="list-info">' + car.ds +
                        '</p><p class="list-price">' + car.cp + '万<s>新车' + car.newprice + '万</s></p></a></li>';
	                    $('#list').append(html);
	                }
	                lockajax = false;
	            }
	        })
	    }
	});
});
